package collection;

import java.util.*;

public class SortListDemo2 {
    public static void main(String[] args) {
        List<Point> list = new ArrayList<>();
        list.add(new Point(5, 6));
        list.add(new Point(13, 87));
        list.add(new Point(55, 35));
        list.add(new Point(9, 4));
        list.add(new Point(2, 1));
        list.add(new Point(99, 88));
        System.out.println(list);


        /*
//        Collections.sort(list);//如果修改point类实现comparator接口，则使用该API有侵入性，不利于后期维护
        Comparator<Point> c=new Comparator<Point>() {
            @Override
            public int compare(Point o1, Point o2) {
                int olen1=o1.getX()*o1.getX()+o1.getY()*o1.getY();
                int olen2=o2.getX()*o2.getX()+o2.getY()*o2.getY();
                return -(olen1-olen2);//降序
//                return olen1-olen2;//升序
            }
        };
        Collections.sort(list,c);

         */
        //采用lambda表达式
        Collections.sort(list, (o1, o2) ->
                o1.getX() * o1.getX() + o1.getY() * o1.getY() - (o2.getX() * o2.getX() + o2.getY() * o2.getY())
        );
        System.out.println(list);


    }
}
